use hypers::prelude::*;
use hypers::tracing::info;

#[hook]
async fn set_user(mut req: Request, next: Next<'_>) -> Response {
    req.depot.insert("user", "client");
    next.next(req).await
}

async fn hello(req: Request) -> String {
    format!("Hello {}", req.depot.get::<&str>("user").unwrap())
}

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt().init();
    let mut root = Router::default();
    root.hook(set_user, None, None);
    root.get("/hello", hello);
    info!("root = {:#?}", root); // http://127.0.0.1:7878/hello
    let listener = hypers::TcpListener::bind("127.0.0.1:7878").await?;
    hypers::listen(root, listener).await
}
